package jwt;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.junit.Test;

import java.util.Date;

public class CreateJwtTest3 {
    @Test
    //创建token
    public void test1(){
        JwtBuilder jwtBuilder = Jwts.builder()
                .setId("1001")
                .setSubject("tom")
                .setIssuedAt(new Date())
                .signWith(SignatureAlgorithm.HS256, "doublefuns")
                .claim("role","admin");

        System.out.println(jwtBuilder.compact());

    }

    @Test
    //解析token
    public void test2(){
        String token = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIxMDAxIiwic3ViIjoidG9tIiwiaWF0IjoxNTcwNDE2NjAxLCJyb2xlIjoiYWRtaW4ifQ.RQgP2I1gWKrgxpM5vKeVAG8yF8Jsdn1-KyqKSZQ73No";
        Claims claims = Jwts.parser().setSigningKey("doublefuns").parseClaimsJws(token).getBody();

        System.out.println("jti:"+claims.getId());
        System.out.println("sub:"+claims.getSubject());
        System.out.println("jat:"+claims.getIssuedAt());
        System.out.println("claim:"+claims.get("role"));
    }
}
